/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_rt_prog_ctx.h"
#include "nu_stmt_end_for.h"
#include "nu_error_codes.h"
#include "nu_lang_defs.h"

/* -------------------------------------------------------------------------- */

namespace nu
{

// -------------------------------------------------------------------------

stmt_end_for_t::stmt_end_for_t(prog_ctx_t & ctx) :
   stmt_t(ctx)
{
   syntax_error_if(ctx.compiletime_pc.get_stmt_pos() > 0,
                   "} \"for\" statement must be a first statement of the line");

   auto handle = ctx.for_loop_metadata.end_find(ctx.compiletime_pc);

   if (!handle)
      handle = ctx.for_loop_metadata.compile_end(ctx.compiletime_pc);

   if (handle)
      handle->pc_end_stmt = ctx.compiletime_pc;

   ctx.parsing_scope.pop();
}


// -------------------------------------------------------------------------

void stmt_end_for_t::run(rt_prog_ctx_t & ctx)
{
   auto handle = ctx.for_loop_metadata.end_find(ctx.runtime_pc);

   rt_error_code_t::get_instance().throw_if(
      !handle,
      ctx.runtime_pc.get_line(),
      rt_error_code_t::E_NO_MATCH_FOR,
      "} for");

   //Loop condition check
   if (! handle->flag[instrblock_t::EXIT])
   {
      handle->increment_stmt->run(ctx);

      //jump to for statement
      ctx.go_to(handle->pc_begin_stmt);
   }

   else
   {
      //LOOP completed, go to next line
      ctx.go_to_next();

      std::string for_id =
         "$"PROGLANG_STMT_FOR"$" + handle->pc_begin_stmt.to_string();

            var_scope_t::handle_t scope =
               ctx.proc_scope.get(proc_scope_t::type_t::LOCAL);

            scope->erase(for_id);
        }
    }


    // -------------------------------------------------------------------------

    stmt_t::stmt_cl_t stmt_end_for_t::get_cl() const throw()
    {
        return stmt_cl_t::FOR_END;
    }
}

/* -------------------------------------------------------------------------- */

